package com.supms.course.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.TeachPlanRecordMapper;
import com.supms.course.domain.TeachPlanRecord;
import com.supms.course.service.ITeachPlanRecordService;

/**
 * 教案检查Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-03
 */
@Service
public class TeachPlanRecordServiceImpl implements ITeachPlanRecordService 
{
    @Autowired
    private TeachPlanRecordMapper teachPlanRecordMapper;

    /**
     * 查询教案检查
     * 
     * @param id 教案检查主键
     * @return 教案检查
     */
    @Override
    public TeachPlanRecord selectTeachPlanRecordById(Long id)
    {
        return teachPlanRecordMapper.selectTeachPlanRecordById(id);
    }

    /**
     * 查询教案检查列表
     * 
     * @param teachPlanRecord 教案检查
     * @return 教案检查
     */
    @Override
    public List<TeachPlanRecord> selectTeachPlanRecordList(TeachPlanRecord teachPlanRecord)
    {
        String operName = SecurityUtils.getUsername();
        teachPlanRecord.setCreateBy(operName);
        return teachPlanRecordMapper.selectTeachPlanRecordList(teachPlanRecord);
    }

    /**
     * 新增教案检查
     * 
     * @param teachPlanRecord 教案检查
     * @return 结果
     */
    @Override
    public int insertTeachPlanRecord(TeachPlanRecord teachPlanRecord)
    {
        String operName = SecurityUtils.getUsername();
        teachPlanRecord.setCreateBy(operName);
        String supName = teachPlanRecordMapper.getSupNameByOperName(operName);
        teachPlanRecord.setSupName(supName);
        teachPlanRecord.setCreateTime(DateUtils.getNowDate());
        return teachPlanRecordMapper.insertTeachPlanRecord(teachPlanRecord);
    }

    /**
     * 修改教案检查
     * 
     * @param teachPlanRecord 教案检查
     * @return 结果
     */
    @Override
    public int updateTeachPlanRecord(TeachPlanRecord teachPlanRecord)
    {
        String operName = SecurityUtils.getUsername();
        teachPlanRecord.setUpdateBy(operName);
        teachPlanRecord.setUpdateTime(DateUtils.getNowDate());
        return teachPlanRecordMapper.updateTeachPlanRecord(teachPlanRecord);
    }

    /**
     * 批量删除教案检查
     * 
     * @param ids 需要删除的教案检查主键
     * @return 结果
     */
    @Override
    public int deleteTeachPlanRecordByIds(Long[] ids)
    {
        // 判断教案检查表中是否做了教案评价，有则先删除教案评价表
        Long[] haveIds = teachPlanRecordMapper.selectTeachPlanSituationFlagByIds(ids);
        if (haveIds.length > 0) {
            teachPlanRecordMapper.deleteTPSituationByIds(haveIds);
        }
        return teachPlanRecordMapper.deleteTeachPlanRecordByIds(ids);
    }

    /**
     * 删除教案检查信息
     * 
     * @param id 教案检查主键
     * @return 结果
     */
    @Override
    public int deleteTeachPlanRecordById(Long id)
    {
        return teachPlanRecordMapper.deleteTeachPlanRecordById(id);
    }

    /**
     * 获取教案检查记录表（数据综合查看表）
     */
    @Override
    public TeachPlanRecord getTeachPlanTable(TeachPlanRecord teachPlanRecord) {
        // 判断是否已经评价，没有评价的话不查询评价结果
        String situationFlag = teachPlanRecord.getTeachPlanSituationFlag();
        // "0" 为未评价，"1" 为已评价
        if ("0".equals(situationFlag)) {
            return teachPlanRecordMapper.getTeachPlanTable0(teachPlanRecord);
        } else if ("1".equals(situationFlag)) {
            return teachPlanRecordMapper.getTeachPlanTable1(teachPlanRecord);
        } else return null;
    }
}
